package xin.yangda.poiplus.test.controller;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.yangda.poiplus.constants.PoiPlusFileExtend;
import xin.yangda.poiplus.designer.MultipleXlsDesigner;
import xin.yangda.poiplus.pojo.ExcelData;
import xin.yangda.poiplus.test.entity.OrderEntity;
import xin.yangda.poiplus.test.entity.UserEntity;
import xin.yangda.poiplus.test.service.OrderService;
import xin.yangda.poiplus.test.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * @author yangdaxin
 * @version 创建时间 2019/1/21 16:32
 */
@RestController
@RequestMapping(value = "/userOrder", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UserOrderController {
    private static final String XLS_EXCEL_TEMP_PATH = "templates/userOrder.xls";
    private static final String XLSX_EXCEL_TEMP_PATH = "templates/userOrder.xlsx";

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    /**
     * 下载excel（有模板）
     *
     * @param request
     * @param response
     */
    @GetMapping(value = "/excelByTemplate")
    public void exportExcelTemp(HttpServletRequest request, HttpServletResponse response) {
        // 打开模板
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream(XLS_EXCEL_TEMP_PATH);
        //InputStream inputStream = getClass().getClassLoader().getResourceAsStream(XLSX_EXCEL_TEMP_PATH);
        try (MultipleXlsDesigner multipleXlsDesigner = new MultipleXlsDesigner(inputStream)) {
            // 填充数据源
            ExcelData<UserEntity> userData = new ExcelData<>(UserEntity.class, userService.queryList());
            ExcelData<OrderEntity> orderData = new ExcelData<>(OrderEntity.class, orderService.queryList());

            List<ExcelData> excelDataList = Lists.newArrayList();
            excelDataList.add(userData);
            excelDataList.add(orderData);

            multipleXlsDesigner.setData(excelDataList);

            // 数据加工
            multipleXlsDesigner.process();

            // 保存Excel
            multipleXlsDesigner.download(request, response, PoiPlusFileExtend.XLS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 下载excel（无模板）
     *
     * @param request
     * @param response
     */
    @GetMapping(value = "/excel")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
        try (MultipleXlsDesigner multipleXlsDesigner = new MultipleXlsDesigner()) {
            // 填充数据源
            ExcelData<UserEntity> userData = new ExcelData<>(UserEntity.class, userService.queryList());
            ExcelData<OrderEntity> orderData = new ExcelData<>(OrderEntity.class, orderService.queryList());

            List<ExcelData> excelDataList = Lists.newArrayList();
            excelDataList.add(userData);
            excelDataList.add(orderData);

            multipleXlsDesigner.setData(excelDataList);

            // 数据加工
            multipleXlsDesigner.process();

            // 保存Excel
            multipleXlsDesigner.download(request, response, PoiPlusFileExtend.XLS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 下载模板
     *
     * @param request
     * @param response
     */
    @GetMapping(value = "/template")
    public void exportTemplate(HttpServletRequest request, HttpServletResponse response) {
        try (MultipleXlsDesigner multipleXlsDesigner = new MultipleXlsDesigner()) {
            // 填充数据源
            ExcelData<UserEntity> userData = new ExcelData<>(UserEntity.class);
            ExcelData<OrderEntity> orderData = new ExcelData<>(OrderEntity.class);

            List<ExcelData> excelDataList = Lists.newArrayList();
            excelDataList.add(userData);
            excelDataList.add(orderData);

            multipleXlsDesigner.setData(excelDataList);

            // 数据加工
            multipleXlsDesigner.process(true);

            // 保存Excel
            multipleXlsDesigner.download(request, response, PoiPlusFileExtend.XLS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
